﻿using System.Net;
using BloggerAutoPosterService.Engine.Core;

namespace BloggerAutoPosterService.Engine.Net
{
    public class ProxySettings
    {
        private static IWebProxy getCurrentProxy(IServiceConfig serviceConfig)
        {
            if (!serviceConfig.IsProxyEnabled) return WebRequest.DefaultWebProxy;

            var myProxy = new WebProxy(serviceConfig.ProxyServerAddress, serviceConfig.ProxyServerPort)
            {
                BypassProxyOnLocal = true
            };
            var myCred = new NetworkCredential(serviceConfig.ProxyServerUserName, serviceConfig.ProxyServerPassword);
            myProxy.Credentials = myCred;
            return myProxy;
        }

        public static void SetDefaultProxy(IServiceConfig serviceConfig)
        {
            if (!serviceConfig.IsProxyEnabled) return;
            WebRequest.DefaultWebProxy = getCurrentProxy(serviceConfig);
        }
    }
}
